<template>
  <div class="form-container">
    <div class="title-wrap">
      <a-row>
        <a-col :span="18" :offset="3">
          <span class="title">密码找回</span>
        </a-col>
      </a-row>
    </div>
    <CustomForm
      :formRootInstance="formRootInstance"
      :formRules="formRules"
      :initModel="initModel"
      :tempModel="tempModel"
      :submitButtonOptions="submitButtonOptions"
      :curCaptchaPath="curCaptchaPath"
      @refreshAuthCode="getCaptchaPath"
      @submit="handleSubmitForm"
    >
      <template v-slot:footer>
        <a-row>
          <a-col :span="8" :offset="3">
            <a class="a-tag-init" @click="login">登录已有账号</a>
          </a-col>
          <a-col :span="6" :offset="6">
            <a class="a-tag-init" @click="backHome">返回首页</a>
          </a-col>
        </a-row>
      </template>
      <template v-slot:footer-tips>
        <a-row>
          <a-col :span="18" :offset="3">
            <span class="tips-title">提示: </span>
            <span class="tips-content"
              >系统将自动向邮箱发送一份邮件，请根据提示进行重置密码操作。</span
            >
          </a-col>
        </a-row>
      </template>
    </CustomForm>
    <Loading :loading="loading" />
  </div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import CustomForm from '@/components/CustomForm/index'
import Loading from '@/components/Loading/index'
import mixinConfigSetting from '@/mixins/mixinConfigSetting'
import mixinLogin from '../mixins/index'
import { captchaImage, forgetPwd } from '@/api/user'

export default {
  mixins: [mixinConfigSetting, mixinLogin],
  components: {
    CustomForm,
    Loading
  },
  props: {
    formConfig: Object
  },
  data() {
    return {

    }
  },
  computed: {
    ...mapGetters('user', [
      'role',
      'account',
      'visitor',
      'token'
    ])
  },
  mounted() {
    // 验证码
    this.getCaptchaImage()
  },
  methods: {
    ...mapMutations('user', [
      'SET_ACCOUNT',
      'SET_ROLE',
      'SET_TOKEN'
    ]),
    login() {
      let path = '/platformPortal'
      this.$router.push({ path: path })
    },
    getCaptchaImage() {
      captchaImage().then((res) => {
        if (res.code === 200) {
          this.curCaptchaPath = `data:image/gif;base64,${res.img}`
          this.uuid = res.uuid
        }
      })
    },
    getCaptchaPath() {
      this.getCaptchaImage()
    },
    handleSubmitForm(formValue) {
      this.callWithHandlerError(async () => {
        const { form } = formValue
        this.normalizeLoginForm(form)

      })
    },
    normalizeLoginForm(form) { // 标准化参数
      form.uuid = this.uuid
      forgetPwd(form).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.msg)
          this.cbOperation('login')
        } else {
          this.getCaptchaPath() // 重新获取验证码
          this.$message.warning(res.msg)
          this.loading = false
        }
      })
    }
  }
}
</script>